package com.fjwt.gz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjwt.gz.core.exception.BizException;
import com.fjwt.gz.db.dto.LiteBannerDTO;
import com.fjwt.gz.db.entity.AgentInfoEntity;
import com.fjwt.gz.db.entity.LiteBannerEntity;
import com.fjwt.gz.db.vo.LiteBannerVO;
import com.fjwt.gz.service.mapper.LiteBannerMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 小程序轮播图配置表 服务实现类
 * </p>
 *
 * @author shiyonghao
 * @since 2025-04-09
 */
@Slf4j
@Service
public class LiteBannerService extends ServiceImpl<LiteBannerMapper, LiteBannerEntity> {

    @Autowired
    private LiteBannerMapper liteBannerMapper;

    @Autowired
    private AgentInfoService agentInfoService;

    public IPage<LiteBannerVO> selectLiteBannerList(IPage<?> page, LiteBannerDTO dto) {
        String agentNo = dto.getAgentNo();
        AgentInfoEntity agentInfoEntity = agentInfoService.getAgentInfo(agentNo);
        if (agentInfoEntity != null) {
            dto.setLevel(agentInfoEntity.getLevel())
                    .setAgentNo1(agentInfoEntity.getAgentNo1())
                    .setAgentNo2(agentInfoEntity.getAgentNo2())
                    .setAgentNo3(agentInfoEntity.getAgentNo3())
                    .setAgentNo4(agentInfoEntity.getAgentNo4());
        }
        return liteBannerMapper.selectLiteBannerList(page, dto);
    }

    public IPage<LiteBannerEntity> selectLiteBannerListPC(IPage<LiteBannerEntity> iPage, LiteBannerDTO dto) {
        List<String> agentNos =null;
        if (!"0".equals(dto.getCurrentAgentNo())){
            agentNos = agentInfoService.queryAllSubAgentNo(dto.getCurrentAgentNo());
        }
        return baseMapper.selectPage(iPage, LiteBannerEntity.gw()
                .eq(StrUtil.isNotBlank(dto.getAppId()), LiteBannerEntity::getAppId, dto.getAppId())
                .in(ObjectUtils.isNotEmpty(agentNos), LiteBannerEntity::getAgentNo, agentNos)
                .like(StrUtil.isNotBlank(dto.getSwiperName()), LiteBannerEntity::getSwiperName, dto.getSwiperName())
                .eq(LiteBannerEntity::getIsDeleted, 0)
                .eq(dto.getState() != null, LiteBannerEntity::getState, dto.getState()));
    }

    public IPage<LiteBannerEntity> selectLiteBannerListByAgentNo(IPage<?> page, LiteBannerEntity liteBannerEntity, String agentNo) {
        return liteBannerMapper.selectLiteBannerListByAgentNo(page, liteBannerEntity, agentNo);
    }

    /**
     * 轮播图新增
     */
    public void insert(LiteBannerDTO dto, Long sysUserId) {
        AgentInfoEntity agentInfo = agentInfoService.getAgentInfo(dto.getAgentNo());
        if (ObjectUtils.isEmpty(agentInfo)) {
            throw new BizException("请选择正确的代理");
        }
        dto.setAgentNo1(agentInfo.getAgentNo1());
        dto.setAgentNo2(agentInfo.getAgentNo2());
        dto.setAgentNo3(agentInfo.getAgentNo3());
        dto.setAgentNo4(agentInfo.getAgentNo4());
        dto.setCreatedUid(sysUserId);
        dto.setCreatedAt(new Date());
        dto.setIsDeleted(0);
        dto.setState(1);
        LiteBannerEntity entity = BeanUtil.copyProperties(dto, LiteBannerEntity.class);
        try {
            save(entity);
        } catch (Exception e) {
            throw new BizException("新增失败！");
        }
    }

    /**
     * 轮播图详情
     */
    public LiteBannerVO selectDetailById(Long bannerId) {
        LiteBannerEntity entity = getById(bannerId);
        LiteBannerVO vo = BeanUtil.copyProperties(entity, LiteBannerVO.class);
        return vo;
    }

    /**
     * 轮播图修改
     */
    public void updateBannerById(LiteBannerDTO dto, Long sysUserId) {
        LiteBannerEntity old = getById(dto.getBannerId());
        if (old.getState() == 0) {
            throw new BizException("请先下线再修改");
        }
        if (ObjectUtils.notEqual(old.getAgentNo(), dto.getAgentNo())) {
            AgentInfoEntity agentInfo = agentInfoService.getAgentInfo(dto.getAgentNo());
            if (ObjectUtils.isEmpty(agentInfo)) {
                throw new BizException("请选择正确的代理");
            }
            dto.setAgentNo1(agentInfo.getAgentNo1());
            dto.setAgentNo2(agentInfo.getAgentNo2());
            dto.setAgentNo3(agentInfo.getAgentNo3());
            dto.setAgentNo4(agentInfo.getAgentNo4());
        }
        LiteBannerEntity entity = BeanUtil.copyProperties(dto, LiteBannerEntity.class);
        entity.setUpdateAt(new Date());
        entity.setState(1);
        entity.setUpdatedUid(sysUserId);
        updateById(entity);
    }

    /**
     * 轮播图上下线
     */
    public void updateStateById(LiteBannerDTO dto, Long sysUserId) {
        if (dto.getState() == 0) {
            LiteBannerEntity entity = getById(dto.getBannerId());

            if ("1".equals(entity.getEndType())) {
                if (entity.getEndTime().compareTo(new Date()) < 0) {
                    throw new BizException("过期时间应大于上线时间");
                }
            }
        }
        LiteBannerEntity entity = new LiteBannerEntity();
        entity.setBannerId(dto.getBannerId());
        entity.setUpdateAt(new Date());
        entity.setState(dto.getState());
        entity.setUpdatedUid(sysUserId);
        updateById(entity);

    }

    /**
     * 轮播图删除
     */
    public void updateIsDelById(Long bannerId) {
        LiteBannerEntity entity = getById(bannerId);
        if (entity.getState() == 0) {
            throw new BizException("请先下线再删除");
        }
        LiteBannerEntity liteBannerEntity = new LiteBannerEntity();
        liteBannerEntity.setBannerId(bannerId);
        liteBannerEntity.setIsDeleted(1);
        updateById(liteBannerEntity);
    }
}
